package main

// import "errors"

type Dictionary map[string]string

type DictionaryErr string

const (
    ErrNotFound = DictionaryErr("could not find the word you were looking for")
    ErrWordExists = DictionaryErr("cannot add word because it already exists")
    ErrWordDoesNotExist = DictionaryErr("cannot update word because it does not exists")
)

func (e DictionaryErr) Error() string {
    return string(e)
}

func (d Dictionary) Search(word string) (string, error) {
    definition, ok := d[word]
    if !ok {
        return "", ErrNotFound 
    }

    return definition, nil
}

func (d Dictionary) Add(word string, definition string) error {
    _, err := d.Search(word)

    switch err {
    case ErrNotFound:
        d[word] = definition
    case nil:
        return ErrWordExists
    default:
        return err
    }
    return nil

}

func (d Dictionary) update(word string, newDefinition string) error {
    _, err := d.Search(word)
    switch err {
    case ErrNotFound:
        return ErrWordDoesNotExist
    case nil:
        d[word] = newDefinition
    default:
        return err
    }

    return nil
}

func (d Dictionary) Delete(word string) {
    delete(d, word)
}
